package homes;

import java.io.Serializable;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

@SuppressWarnings("serial")
public abstract class GenericHome<T> extends HibernateDaoSupport implements Serializable{

	   protected Class<T> persistentClass = this.getDomainClass();

	   protected abstract Class<T> getDomainClass();
	   
	    @SuppressWarnings("unchecked")
	    public int count() {
	        List<Long> list = this.getHibernateTemplate().find(
	                "select count(*) from " + this.persistentClass.getName() + " o");
	        Long count = list.get(0);
	        return count.intValue();
	    }

	    public T findById(final Serializable id) {
	        return this.getHibernateTemplate().get(this.persistentClass, id);
	    }

	    @SuppressWarnings("unchecked")
	    public List<T> findByExample(final T exampleObject) {
	        return this.getHibernateTemplate().findByExample(exampleObject);

	    }

	    @SuppressWarnings("unchecked")
	    public List<T> findAll() {
	        return this.getHibernateTemplate().find("from " + this.persistentClass.getName() + " o");
	    }

	    public void delete(final T entity) {
	        this.getHibernateTemplate().delete(entity);
	    }

	    public void deleteById(final Serializable id) {
	        T obj = this.findById(id);
	        this.getHibernateTemplate().delete(obj);
	    }

	    public void save(final T entity) {
	        this.getHibernateTemplate().save(entity);
	        this.getHibernateTemplate().flush();
	    }

	    public void update(final T entity) {
	        this.getHibernateTemplate().update(entity);
	    }
	
}
